/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tongxunlu;

import java.io.*;
import javax.swing.*;

/**
 *
 * @author Tim
 */
public class VCardOP {
     public static boolean WriteVcard(String File, person p) {
        boolean ErrorFlag = false;

        try {

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(File), "GBK"));



            bw.write("BEGIN:VCARD\r\n");
            bw.write("VERSION:2.1\r\n");
            bw.write("FN:" + p.getNAME() + "\r\n");
            bw.write("ORG:" + p.getWORKMASSAGE().getWORKUNIT() + ";" + p.getWORKMASSAGE().getDEPARTMENT() + "\r\n");
            bw.write("TITLE:" + p.getWORKMASSAGE().getPOSITION() + "\r\n");
            bw.write("NOTE:" + "备注 " + p.getREMARKS() +  ";" + "所属组 " + p.getGROUP() + ";" + "QQ号码 " + p.getNETMASSAGE().getQQNUM() + ";" + "备用邮箱 " + p.getNETMASSAGE().getBACKUPEMAIL() + "\r\n");
            bw.write("TEL;WORK;VOICE:" + p.getWORKMASSAGE().getWORKTEL() + "\r\n");
            bw.write("TEL;HOME;VOICE:" + p.getHOMEMASSAGE().getHOMETEL() + "\r\n");
            bw.write("TEL;CELL;VOICE:" + p.getTEL() + "\r\n");
            bw.write("TEL;WORK;FAX:" + p.getWORKMASSAGE().getWORKFAX() + "\r\n");
            bw.write("ADR;WORK:;;" + p.getWORKMASSAGE().getWORKADD() + ";" + p.getWORKMASSAGE().getWORKCITY() + ";" + p.getWORKMASSAGE().getWORKPROVINCE() + ";" + p.getWORKMASSAGE().getWORKZIPCODE() + ";" + p.getWORKMASSAGE().getWORKCOUNTRY() + "\r\n");
            bw.write("ADR;HOME:;;" + p.getHOMEMASSAGE().getHOMEADD() + ";" + p.getHOMEMASSAGE().getHOMECITY() + ";" + p.getHOMEMASSAGE().getHOMEPROVINCE() + ";" + p.getHOMEMASSAGE().getHOMEZIPCODE() + ";" + p.getHOMEMASSAGE().getHOMECOUNTRY() + "\r\n");
            bw.write("URL;HOME:" + p.getNETMASSAGE().getHOMEPAGE() + "\r\n");
            bw.write("EMAIL;PREF;INTERNET:" + p.getEMAIL() + "\r\n");
            bw.write("REV:" + p.getBIR() + "T082617Z\r\n");
            bw.write("END:VCARD");

            bw.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "导出Vcard出错！");
            e.printStackTrace();
            return ErrorFlag;
        }

        ErrorFlag = true;
        return ErrorFlag;
    }

    public static void ComChooser(String s, person p) {
        String[] linesp1 = new String[2];
        String[] linesp2 = new String[5];
        String[] linesp3 = new String[2];
        linesp1 = s.split(":");
        if (linesp1[0].equals("FN")) {
            p.setNAME(linesp1[1]);
            
            
        } else if (linesp1[0].equals("ORG")) {
            linesp3 = linesp1[1].split(";");
            p.getWORKMASSAGE().setWORKUNIT(linesp3[0]);
            p.getWORKMASSAGE().setDEPARTMENT(linesp3[1]);
        } else if (linesp1[0].equals("TITLE")) {
            p.getWORKMASSAGE().setPOSITION(linesp1[1]);
        } else if (linesp1[0].equals("NOTE")) {
            if (linesp1[1].contains("备注")) {
                linesp2 = linesp1[1].split(";");
                linesp1 = linesp2[0].split(" ");
                p.setREMARKS(linesp1[1]);
                linesp1 = linesp2[1].split(" ");
                p.setGROUP(linesp1[1]);
                linesp1 = linesp2[2].split(" ");
                p.getNETMASSAGE().setQQNUM(linesp1[1]);
                linesp1 = linesp2[3].split(" ");
                p.getNETMASSAGE().setBACKUPEMAIL(linesp1[1]);
                
            } else {
                p.setREMARKS(linesp1[1]);
                p.getNETMASSAGE().setBACKUPEMAIL("无");
                p.getNETMASSAGE().setQQNUM("无");
                p.setGROUP("无");
            }
        } else if (linesp1[0].equals("TEL;WORK;VOICE")) {
            p.getWORKMASSAGE().setWORKTEL(linesp1[1]);
        } else if (linesp1[0].equals("TEL;HOME;VOICE")) {
            p.getHOMEMASSAGE().setHOMETEL(linesp1[1]);
        } else if (linesp1[0].equals("TEL;CELL;VOICE")) {
            p.setTEL(linesp1[1]);
        } else if (linesp1[0].equals("TEL;WORK;FAX")) {
            p.getWORKMASSAGE().setWORKFAX(linesp1[1]);
        } else if (linesp1[0].equals("ADR;WORK")) {
            linesp1 = s.split(":;;");
            linesp2 = linesp1[1].split(";");
            p.getWORKMASSAGE().setWORKADD(linesp2[0]);
            p.getWORKMASSAGE().setWORKCITY(linesp2[1]);
            p.getWORKMASSAGE().setWORKPROVINCE(linesp2[2]);
            p.getWORKMASSAGE().setWORKZIPCODE(linesp2[3]);
            p.getWORKMASSAGE().setWORKCOUNTRY(linesp2[4]);
        } else if (linesp1[0].equals("ADR;HOME")) {
            linesp1 = s.split(":;;");
            linesp2 = linesp1[1].split(";");
            p.getHOMEMASSAGE().setHOMEADD(linesp2[0]);
            p.getHOMEMASSAGE().setHOMECITY(linesp2[1]);
            p.getHOMEMASSAGE().setHOMEPROVINCE(linesp2[2]);
            p.getHOMEMASSAGE().setHOMEZIPCODE(linesp2[3]);
            p.getHOMEMASSAGE().setHOMECOUNTRY(linesp2[4]);
        } else if (linesp1[0].equals("URL;HOME")) {
            p.getNETMASSAGE().setHOMEPAGE(linesp1[1]);
        } else if (linesp1[0].equals("EMAIL;PREF;INTERNET")) {
            p.setEMAIL(linesp1[1]);
        } else if (linesp1[0].equals("REV")) {
            if (linesp1[1].contains("T082617Z")) {
                linesp1 = linesp1[1].split("T082617Z");
                p.setBIR(linesp1[0]);
            } else {
                p.setBIR("无");
            }
        }
    }

    public static boolean ReadVcard(String File, person p) {
        boolean ErrorFlag = false;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(File), "GBK"));

            String[] line = new String[100];
            String getline = null;
            int i = 0;

            while ((getline = br.readLine()) != null) {
                line[i] = getline;
                i++;
            }
//            p.PersonBase("无","不详", "无", "无", "无", "无", "无", p);
//            p.PersonWork("无","无", "无", "无", "无", "无", "无", "无", "无", "无", p);
//            p.PersonInternet( "无", "无", "无", p);
//            p.PersonFamily("无","无", "无", "无", "无", "无", p);
//            p.setPhoto("无");
//            p.setPinyin("无");
            for (int j = 0; j < i; j++) ComChooser(line[j], p);
//                        String[] linesp1 = new String[2];
//                        String[] linesp2 = new String[5];
//                        linesp1 = line[2].split(":");
//                        p.setName(linesp1[1]);
//                        linesp1 = line[3].split(":");
//                        linesp1 = linesp1[1].split(";");
//                        p.setWorkplace(linesp1[0]);
//                        p.setWdepartment(linesp1[1]);
//                        linesp1 = line[4].split(":");
//                        p.setWprosition(linesp1[1]);
//                        linesp1 = line[5].split(":");
//                        linesp2 = linesp1[1].split(";");
//                        linesp1 = linesp2[0].split(" ");
//                        p.setRemark(linesp1[1]);
//                        linesp1 = linesp2[1].split(" ");
//                        p.setSex(linesp1[1]);
//                        linesp1 = linesp2[2].split(" ");
//                        p.setGroup(linesp1[1]);
//                        linesp1 = linesp2[3].split(" ");
//                        p.setQQnumber(linesp1[1]);
//                        linesp1 = linesp2[4].split(" ");
//                        p.setSecondemail(linesp1[1]);
//                        linesp1 = line[6].split(":");
//                        p.setWorkphone(linesp1[1]);
//                        linesp1 = line[7].split(":");
//                        p.setFphone(linesp1[1]);
//                        linesp1 = line[8].split(":");
//                        p.setMphonenumber(linesp1[1]);
//                        linesp1 = line[9].split(":");
//                        p.setWfax(linesp1[1]);
//                        linesp1 = line[10].split(":;;");
//                        linesp2 = linesp1[1].split(";");
//                        p.setWaddress(linesp2[0]);
//                        p.setWcity(linesp2[1]);
//                        p.setWprovince(linesp2[2]);
//                        p.setWpostcode(linesp2[3]);
//                        p.setWnation(linesp2[4]);
//                        linesp1 = line[11].split(":;;");
//                        linesp2 = linesp1[1].split(";");
//                        p.setFaddress(linesp2[0]);
//                        p.setFcity(linesp2[1]);
//                        p.setFprovince(linesp2[2]);
//                        p.setFpostcode(linesp2[3]);
//                        p.setFnation(linesp2[4]);
//                        linesp1 = line[12].split(":");
//                        p.setPersonalpage(linesp1[1]);
//                        linesp1 = line[13].split(":");
//                        p.setEmail(linesp1[1]);
//                        linesp1 = line[14].split(":");
//                        linesp1 = linesp1[1].split("T082617Z");
//                        p.setBirthday(linesp1[0]);
//                        p.setPinyin(Pinyin.getFullSpell(p.getName()));
//                        p.setPhoto(Pinyin.getFullSpell(p.getName()) + ".jpg");
            
                br.close();
            
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "导入Vcard错误！");
            e.printStackTrace();
            return ErrorFlag;
        }


        ErrorFlag = true;
        return ErrorFlag;
    }
}
